
var title = $('div.container > div.col2 > h1').html();

function nbl_handler(){
	if(GM_safeVariables["ds_buildingRuleCurrent"] != null && GM_safeVariables["ds_buildingRuleCurrent"] != ""){
        var currentBuildingRule = GM_safeVariables["ds_buildingRuleCurrent"];
        log("針對建筑物'"+currentBuildingRule.buildingName+"',進入建造/升級過程...");
        var link = null;
        //find out the "建造" link
        if(title == "未建造建築物列表"){
            var titleH2s = $("div.b_block > h2");
            link = $('div.b_block > h2:contains("'+currentBuildingRule.buildingName+'") ~ div.b_info > p > a:contains("建造")').get(0);
        }else{
            var links = $('div.b_info>p>a:contains("建造")');
            if(links.length > 0) {
                link = links.get(0);
                if (link.innerHTML != '建造') {
                    link = null;
                }
            }
        }
        //remove rule from building rule array.
        CONFIG.building_rules.removeByObjectProperty("id",currentBuildingRule.id);
        //remove identity variable      
        delete GM_safeVariables["ds_buildingRuleCurrent"];
        if(link != null){
            var randomTime = Math.random()*5000;
            randomTime = randomTime<2000?2000:randomTime;
            window.setTimeout(function(){
                var evt = document.createEvent("MouseEvents");  
                evt.initEvent("click",true,true);  
                link.dispatchEvent(evt);
            },randomTime);
        }else{
            log("無法滿足當前建筑步驟的要求,或許您的建筑順序安排有誤,如不滿足本建筑的先決條件,或者建筑正在進行中，或資源不足,將當前步驟放入隊列最后,以便稍后執行...");
            if(currentBuildingRule["failed"] == null){
              currentBuildingRule["failed"] = 1;        
              CONFIG.building_rules.push(currentBuildingRule);
            }else{
              var failedCount =  parseInt(currentBuildingRule["failed"]);
              if(failedCount < 3){
                currentBuildingRule["failed"] = (failedCount+1).toString();
                CONFIG.building_rules.push(currentBuildingRule);
              }else{
                log("當前建筑步驟失敗超過3次,自動從隊列中刪除...");
              }
            }
            clickLink("/mindex");
        }
    }
	clickLink('/mindex');
    return;
}

function nbl_handler_configMode(){
	var cityName = jQuery.trim($('ul.city_list > li.on > a').attr('title'));
    //create patch for server bug.
    if(cityName.indexOf('(主城)')>-1)
        cityName = jQuery.trim(cityName.replace('(主城)',''));
    var ds_cfg_addBuildingRule = null;
    var buildingName = null;

	if(title == "未建造建築物列表"){
        $('div.col2 > div.b_block > div.b_info').append('<p>'+getDSIconPath()+'<a href="javascript:{void(0);}" class="ds_cfg_addBuildingRule">加入屠龍者自動建筑隊列</a></p>');
    }else{
        $('div.col2 > div.b_block > div.b_info').append('<p>'+getDSIconPath()+'<a href="javascript:{void(0);}" class="ds_cfg_addBuildingRule">加入屠龍者自動建筑隊列</a></p>');
        buildingName = jQuery.trim($('div.col2 > h1').html().split('<span>')[0]);
    }

	 $('a.ds_cfg_addBuildingRule').click(function(){
		 if(buildingName == null){
            buildingName = $(this).parent().parent().siblings('h2').html();
        }
		var id = new Date().getTime();
        var rule = new Object();
        rule.id = id;
        rule.cityName = cityName;
        rule.buildingName = buildingName;
        CONFIG.building_rules.push(rule);
        $(this).replaceWith('<span>成功加入屠龍者自動建筑隊列</span>');
        buildingName = null;
	 })
}